﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EnemyManager : MonoBehaviour {
    // 单例模式
    public static EnemyManager _instance;

    public EnemySpawn[] monsterSpawn;
    public EnemySpawn[] bossSpawn;
    public EnemySpawn firstBoss;

    public List<GameObject> enemyListArray=new List<GameObject>();

    public AudioClip gameVctory;

    void Awake()
    {
        _instance = this;
    }

	// Use this for initialization
	void Start () {
        StartCoroutine(CreateSpawn());
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    IEnumerator CreateSpawn()
    {
        //第一波
        enemyListArray.Add(monsterSpawn[0].EnemyPrefab());
        enemyListArray.Add(monsterSpawn[1].EnemyPrefab());

        while (enemyListArray.Count > 0)
        {
            yield return new WaitForSeconds(2.0f);
        }

        // 第二波敌人生成
        // 一般情况下,遍历数组或者集合时用foreach
        // 修改数组或者集合时用for
        foreach (EnemySpawn spwn in monsterSpawn)
        {
            enemyListArray.Add(spwn.EnemyPrefab());
        }

        while (enemyListArray.Count > 0 )
        {
            yield return new WaitForSeconds(2.0f);
        }

            // 第三波敌人生成
            foreach (EnemySpawn spwn in monsterSpawn)
            {
                enemyListArray.Add(spwn.EnemyPrefab());
            }


        yield return new WaitForSeconds(2f);

        foreach (EnemySpawn spwn in monsterSpawn)
        {
            enemyListArray.Add(spwn.EnemyPrefab());
        }

        enemyListArray.Add(bossSpawn[0].EnemyPrefab());

        while (enemyListArray.Count > 0)
        {
            yield return new WaitForSeconds(2.0f);
        }

        yield return new WaitForSeconds(3.0f);

        // 第三波敌人生成
        foreach (EnemySpawn spwn in bossSpawn)
        {
            enemyListArray.Add(spwn.EnemyPrefab());
        }

        yield return new WaitForSeconds(5f);

        foreach (EnemySpawn spwn in bossSpawn)
        {
            enemyListArray.Add(spwn.EnemyPrefab());
        }




        while (enemyListArray.Count > 0)
        {
            yield return new WaitForSeconds(2.0f);
        }

        // 最后一波
        // 生成最后Boss
        enemyListArray.Add(firstBoss.EnemyPrefab());

        foreach (EnemySpawn spwn in bossSpawn)
        {
            enemyListArray.Add(spwn.EnemyPrefab());
        }


        while (enemyListArray.Count > 0)
        {
            yield return new WaitForSeconds(2.0f);
        }

        AudioSource.PlayClipAtPoint(gameVctory, transform.position, 1.0f);



        yield return 0;
    }

}
